<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8">
      <el-card shadow="hover">
        <div class="user">
          <img :src="userImg" class="userImg" />
          <div class="userInfo">
            <p class="name">Admin</p>
            <p class="access">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>
            上次登陆时间:<span
              >{{ this.year }}-{{ this.month }}-{{ this.day }}</span
            >
          </p>
          <p>上次登录地点:<span>西安</span></p>
        </div>
      </el-card>
      <el-card shadow="hover" style="margin-top: 20px">
        <el-table :data="tableData">
          <el-table-column prop="name" label="手机型号" width="140">
          </el-table-column>
          <el-table-column prop="totalBuy" label="总购买" width="140">
          </el-table-column>
          <el-table-column prop="monthBuy" label="本月购买"> </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col class="col" :span="16">
      <el-card
        class="card"
        v-for="item in countData"
        :key="item.name"
        shadow="hover"
      >
        <div class="on">
          <div
            class="icon"
            :class="`el-icon-` + item.icon"
            :style="{ background: item.color }"
          ></div>
          <div>
            <div class="num">¥{{ item.value }}</div>
            <div class="txt">{{ item.name }}</div>
          </div>
        </div>
      </el-card>
      <el-card class="chart" style="height: 225px" shadow="hover">
        <div style="height: 225px" ref="echarts"></div>
      </el-card>
      <div class="graph">
        <el-card style="height: 200px" shadow="hover">
          <div ref="userEcharts" style="height: 200px"></div>
        </el-card>
        <el-card style="height: 200px" shadow="hover">
          <div style="height: 200px" ref="videoEcharts"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { getData } from "../api/data.js";
import * as echarts from "echarts";
export default {
  name: "Home",
  data() {
    return {
      year: new Date().getFullYear(),
      month: new Date().getUTCMonth() + 1,
      day: new Date().getDate(),
      userImg: require("../assets/logo.png"),
      tableData: [],
      countData: [
        {
          name: "今日支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ],
    };
  },
  mounted() {
    getData().then((res) => {
      const { code, data } = res.data;
      if (code === 20000) {
        this.tableData = data.tableData;
        const order = data.orderData;
        const xData = order.date;
        const keyArray = Object.keys(order.data[0]);
        const series = [];
        keyArray.forEach((key) => {
          series.push({
            name: key,
            data: order.data.map((item) => item[key]),
            type: "line",
          });
        });

        const option = {
          // 提示框
          tooltip: {
            triggerOn: "mousemove",
          },
          xAxis: {
            data: xData,
          },
          yAxis: {},
          legend: {
            data: keyArray,
          },
          series,
        };

        const E = echarts.init(this.$refs.echarts);
        E.setOption(option);
        // 用户图
        const userOption = {
          legend: {
            // 图例文字颜色
            textStyle: {
              color: "#333",
            },
          },
          // 提示框
          tooltip: {
            triggerOn: "mousemove",
          },
          xAxis: {
            type: "category",
            data: data.userData.map((item) => item.date),
            axisLine: {
              lineStyle: {
                color: "#17b3a3",
              },
            },
            axisLabel: {
              interval: 0,
              color: "#333",
            },
          },
          yAxis: [
            {
              type: "value",
              axisLine: {
                lineStyle: {
                  color: "#17b3a3",
                },
              },
            },
          ],
          color: ["#2ec7c9", "#b6a2de"],
          series: [
            {
              name: "新增用户",
              data: data.userData.map((item) => item.new),
              type: "bar",
            },
            {
              name: "活跃用户",
              data: data.userData.map((item) => item.active),
              type: "bar",
            },
          ],
        };
        const U = echarts.init(this.$refs.userEcharts);
        U.setOption(userOption);

        const videoOption = {
          // 提示框
          tooltip: {
            triggerOn: "mousemove",
          },
          legend: {
            data: keyArray,
          },
          series: [
            {
              type: "pie",
              data: data.videoData,
            },
          ],
        };
        const V = echarts.init(this.$refs.videoEcharts);
        V.setOption(videoOption);
      }
      console.log(res);
    });
  },
};
</script>

<style>
.userImg {
  width: 100px;
  height: 100px;
  margin-right: 60px;
  background-color: rgb(82, 82, 174);
  border-radius: 50%;
}
.user {
  display: flex;
}
.name {
  font-size: 26px;
  margin-bottom: 0;
}
.access {
  color: #bbb;
  font-size: 14px;
  margin: 0;
}
.login-info p {
  font-size: 12px;
  color: #bbb;
}
.login-info p span {
  color: #000;
  margin-left: 80px;
}
.col {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.card {
  margin-bottom: 10px;
  /* padding-bottom: 40px; */
  width: 30%;
  height: 80px;
}
.on {
  display: flex;
}
.icon {
  width: 30%;
  font-size: 40px;
  text-align: center;
  margin-right: 20px;
}
.num {
  font-size: 20px;
}
.txt {
  color: #aaa;
  font-size: 12px;
}
.chart {
  width: 100%;
}
.graph {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.graph .el-card {
  width: 49%;
  margin-top: 10px;
}
</style>